package com.ls.controller;

import com.ls.dto.EmployeeDto;
import com.ls.pojo.Position;
import com.ls.service.DepartmentService;
import com.ls.service.EmployeeService;
import com.ls.service.PositionService;
import com.ls.utils.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(value = "EmployeeController",tags = "员工管理")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private PositionService positionService;

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "获取员工信息")
    @GetMapping("/employee/getList")
    public Status getListResult(EmployeeDto employeeDto){
        System.out.println(employeeDto);
        return employeeService.getEmployees(employeeDto);
    }

    @ApiOperation(value = "获取单个员工信息")
    @GetMapping("/employee/get")
    public Status getEmployeeResult(EmployeeDto employeeDto){
        return employeeService.select(employeeDto);
    }

    @ApiOperation(value = "删除员工")
    @GetMapping("/employee/delete")
    public Status deleteResult(EmployeeDto employeeDto){

        System.out.println(employeeDto );
        return employeeService.deleteResult(employeeDto);
    }

    @ApiOperation(value = "修改员工信息")
    @PostMapping("/employee/update")
    public Status updateResult(@RequestBody EmployeeDto employeeDto){
        //获取员工部门号
        Integer dep_id = departmentService.getDepId(employeeDto.getDepartmentName());
        //修改员工职位信息
        positionService.update(employeeDto.getPositionId(),employeeDto.getPositionName());
        //添加员工部门号
        employeeDto.setDepId(dep_id);
        return employeeService.updateResult(employeeDto);
    }

    @ApiOperation(value = "修改员工状态")
    @PostMapping("/employee/updStatus")
    public Status updateStatusResult(@RequestBody EmployeeDto employeeDto){

        System.out.println(employeeDto);
        return employeeService.updStatusResult(employeeDto);
    }

    @ApiOperation(value = "添加员工信息")
    @PostMapping("/employee/add")
    public Status addResult(@RequestBody EmployeeDto employeeDto){

        System.out.println("controller"+employeeDto);
        //获取添加职位后的职位id
        Integer positionId = positionService.getPositionId(employeeDto);
        employeeDto.setPositionId(positionId);
        System.out.println(employeeDto);
        return employeeService.addResult(employeeDto);
    }
}
